import Vue from 'vue'
import ax from '../plugins/axios'
import VueRouter from 'vue-router'
import Login from '../components/admin/login.vue'
import Home from "../components/admin/home.vue";
import Welcome from "../components/admin/Welcome.vue";
import List from '../components/admin/list.vue'
import index from "../components/index.vue";
import itemsList from "../views/index/navItems.vue";

const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

Vue.use(VueRouter)


const router = new VueRouter({
  routes: [
    { path: '/', component: index },
    { path: '/list/:id', component: itemsList },
    { path: '/admin', redirect: '/admin/home' }, // 重定向
    { path: '/admin/login', component: Login },
    {
      path: '/admin/home',
      component: Home,
      redirect: '/admin/welcome',
      children: [
        { path: '/admin/welcome', component: Welcome },
        { path: '/admin/items', component: List },
        // { path: '/admin/transactionLog', component: Users },
        // { path: '/admin/itemEval', component: Users },
        // { path: '/admin/shoppingTrolleys', component: Users },
      ]
    }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.path === '/') {
    // console.log(to);
    return next();
  }
  else if (to.path.substr(0, 5) == '/list') return next();
  else if (to.path === '/item') return next();
  else if (to.path === '/admin/login') return next();
  else if (to.path.substr(0, 6) == '/admin') {
    // const token = localStorage.getItem('token');
    // // console.log(ax.axios);
    // ax.axios.post("q.php", { cmd: 1003, val: { token: token } }).then(res => {
    // 	if (res.data) return next();
    // 	return next('/admin/login');
    // });
    if (sessionStorage.getItem('token') && sessionStorage.getItem('token')!='undefined') return next();
    return next('/admin/login');
  }
})

export default router
